package com.welink.cmp.meatdata.command;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.welink.cmp.dao.metadata.EipDaoService;
import com.welink.cmp.dao.metadata.dataobject.EipDO;
import com.welink.cmp.dto.metadata.eip.CreateEipAddCmd;
import com.welink.cmp.dto.metadata.eip.SetupEipUpdateCmd;
import com.welink.cmp.dto.metadata.eip.clientobject.EipCO;
import com.welink.cmp.dto.metadata.eip.query.EipListByPageQry;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * EIP命令执行类
 * 负责处理EIP相关的业务逻辑和数据操作
 */
@Component
public class EipCmdExe {

    @Autowired
    private EipDaoService eipDaoService;

    /**
     * 保存EIP信息
     *
     * @param addCmd 添加EIP的命令对象
     * @return 新增EIP的ID
     */
    public Long save(CreateEipAddCmd addCmd) {
        EipDO eipDO = new EipDO();
        BeanUtils.copyProperties(addCmd, eipDO);
        if (addCmd.isNeedsOperator()){
            eipDO.setCreateBy(addCmd.getOperator());
        }
        eipDaoService.save(eipDO);
        return eipDO.getId();
    }
    /**
     * 根据名称查询Eip信息
     *
     * @param name Eip Name
     * @return Eip 业务对象
     */
    public EipDO checkExistByName(String name) {
        LambdaQueryWrapper<EipDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EipDO::getName,name).eq(EipDO::getIsDel,0);
        final EipDO eipDO = eipDaoService.getOne(wrapper);
        return eipDO;
    }

    /**
     * 更新EIP信息
     *
     * @param updCmd 更新EIP的命令对象
     * @return 更新是否成功
     */
    public Boolean update(SetupEipUpdateCmd updCmd) {
        EipDO eipDO = new EipDO();
        BeanUtils.copyProperties(updCmd, eipDO);
        if (updCmd.isNeedsOperator()){
            eipDO.setUpdateBy(updCmd.getOperator());
        }
        return eipDaoService.updateById(eipDO);
    }

    /**
     * 根据ID查询EIP信息
     *
     * @param id EIP ID
     * @return EIP业务对象
     */
    public EipCO queryById(Long id) {
        EipDO eipDO = eipDaoService.getById(id);
        EipCO eipCO = new EipCO();
        BeanUtils.copyProperties(eipDO, eipCO);
        return eipCO;
    }

    /**
     * 分页查询EIP信息
     *
     * @param pageQry 分页查询条件
     * @return 分页响应，包括EIP列表和分页信息
     */
    public Page<EipDO> pageQuery(EipListByPageQry pageQry) {
        Page<EipDO> eipDOPage = eipDaoService.pageQry(pageQry);
        return eipDOPage;
    }
}